﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.editorControls;

namespace TomMaton.Umbraco.AmazonS3
{
    public class amazonS3PrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
    {
        #region IDataPrevalue Members

        // referenced datatype
        private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;
        private TextBox _txtAccessKeyId;
        private TextBox _txtSecretAccessKeyId;

        public amazonS3PrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType)
        {
            _datatype = DataType;
            setupChildControls();
        }

        private void setupChildControls()
        {
            //Textbox for the Access Key ID
            _txtAccessKeyId = new TextBox();
            _txtAccessKeyId.ID = "txtAccessKeyId";
            _txtAccessKeyId.CssClass = "umbEditorTextField";

            //Textbox for the Secret Access Key ID
            _txtSecretAccessKeyId = new TextBox();
            _txtSecretAccessKeyId.ID = "txtSecretAccessKeyId";
            _txtSecretAccessKeyId.CssClass = "umbEditorTextField";

            Controls.Add(_txtAccessKeyId);
            Controls.Add(_txtSecretAccessKeyId);
        }

        public Control Editor
        {
            get
            {
                return this;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {

                _txtAccessKeyId.Text = amazonS3Functionality.getKey("AccessKeyId", amazonS3Functionality.pluginConfigPath.ToString()); ;
                _txtSecretAccessKeyId.Text = amazonS3Functionality.getKey("SecretAccesKeyId", amazonS3Functionality.pluginConfigPath.ToString());

            }
        }

        public void Save()
        {
            XmlDocument amazonS3Config = new XmlDocument();
            amazonS3Config.Load(amazonS3Functionality.pluginConfigPath.ToString());

            XmlNode getNode = amazonS3Config.SelectSingleNode("/AmazonS3");

            getNode["AccessKeyId"].InnerText = _txtAccessKeyId.Text.ToString();
            getNode["SecretAccesKeyId"].InnerText = _txtSecretAccessKeyId.Text.ToString();

            amazonS3Config.Save(amazonS3Functionality.pluginConfigPath.ToString());
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine("<table>");
            writer.Write("<tr><th>Access Key Id:</th><td>");
            _txtAccessKeyId.RenderControl(writer);
            writer.Write("<tr><th>Secret Access Key Id:</th><td>");
            _txtSecretAccessKeyId.RenderControl(writer);
            writer.Write("</td></tr>");
            writer.Write("</table>");

        }

        public string Configuration
        {
            get
            {
                object conf = SqlHelper.ExecuteScalar<object>("select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid", SqlHelper.CreateParameter("@datatypenodeid", _datatype.DataTypeDefinitionId));

                if (conf != null)
                    return conf.ToString();
                else
                    return "";
            }
        }

        #endregion

        public static ISqlHelper SqlHelper
        {
            get
            {
                return Application.SqlHelper;
            }
        }
    }
}
